package com.onion.codegen.gen;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.onion.codegen.bean.JavaBeanProperties;
import com.onion.codegen.bean.GeneratorProperties;
import com.onion.codegen.utils.TemplateUtil;

public class ControllerGenerator extends BaseGenerator{

	@Override
	public void genJavaClasss(List<JavaBeanProperties> javaPropertiesList, GeneratorProperties javaEntityProperties)
			throws Exception {
		for (JavaBeanProperties item : javaPropertiesList) {
			Map<String, Object> param = new HashMap<>();

			param.put("basePackage", javaEntityProperties.getPackageName());
			param.put("entityBeanImport","import " + javaEntityProperties.getPackageName() + "." + item.getJavaName() + ";");
			param.put("tableDescribe", item.getJavaClassDescribe());
			param.put("author", javaEntityProperties.getAuthor()==null?"":javaEntityProperties.getAuthor());
			param.put("javaEntityName", item.getJavaName());
			String propertieEntityName = item.getJavaName().substring(0, 1).toLowerCase()+item.getJavaName().substring(1, item.getJavaName().length());
			param.put("propertieEntityName", propertieEntityName);
			param.put("apiPathPrefix", item.getDbTable().replace("_", ""));
			
			StringBuffer path = new StringBuffer();
			path.append(javaEntityProperties.getBaseFoler()).append("/");
			path.append(javaEntityProperties.getPackageName().replace(".", "/"));
			path.append("/").append(item.getJavaName()).append("Controller.java");
			
			TemplateUtil.createTemplateFile(javaEntityProperties.isForce(),"controller.ftl", path.toString(), param);
		}
	}
}
